package com.castle.fortress.admin.member.account.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.account.entity.MemberAccountEntity;
import com.castle.fortress.admin.member.account.dto.MemberAccountDto;
import com.castle.fortress.admin.member.account.service.MemberAccountService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.util.List;

/**
 * 会员账户表 控制器
 *
 * @author 
 * @since 2025-04-15
 */
@Api(tags="会员账户表管理控制器")
@Controller
public class MemberAccountController {
    @Autowired
    private MemberAccountService memberAccountService;

    /**
     * 页面
     * @return
     */
/*    @ApiIgnore
    @RequestMapping({"/page/member/account/memberAccount"})
    public String toPage(){
        return "admin/member/account/pages-memberAccount.html";
    }*/

    /**
     * 会员账户表的分页展示
     * @param memberAccountDto 会员账户表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员账户表分页展示")
    @GetMapping("/member/account/memberAccount/page")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:pageList")
    public RespBody<IPage<MemberAccountDto>> pageMemberAccount(MemberAccountDto memberAccountDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberAccountDto> page = new Page(pageIndex, pageSize);
        IPage<MemberAccountDto> pages = memberAccountService.pageMemberAccountExtends(page, memberAccountDto);

        return RespBody.data(pages);
    }

    /**
     * 会员账户表的列表展示
     * @param memberAccountDto 会员账户表实体类
     * @return
     */
    @ApiOperation("会员账户表列表展示")
    @GetMapping("/member/account/memberAccount/list")
    @ResponseBody
    public RespBody<List<MemberAccountDto>> listMemberAccount(MemberAccountDto memberAccountDto){
        List<MemberAccountDto> list = memberAccountService.listMemberAccount(memberAccountDto);
        return RespBody.data(list);
    }

    /**
     * 会员账户表保存
     * @param memberAccountDto 会员账户表实体类
     * @return
     */
    @ApiOperation("会员账户表保存")
    @PostMapping("/member/account/memberAccount/save")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:save")
    public RespBody<String> saveMemberAccount(@RequestBody MemberAccountDto memberAccountDto){
        if(memberAccountDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberAccountEntity memberAccountEntity = ConvertUtil.transformObj(memberAccountDto,MemberAccountEntity.class);
        if(memberAccountService.save(memberAccountEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员账户表编辑
     * @param memberAccountDto 会员账户表实体类
     * @return
     */
    @ApiOperation("会员账户表编辑")
    @PostMapping("/member/account/memberAccount/edit")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:edit")
    public RespBody<String> updateMemberAccount(@RequestBody MemberAccountDto memberAccountDto){
        if(memberAccountDto == null || memberAccountDto.getId() == null || memberAccountDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberAccountEntity memberAccountEntity = ConvertUtil.transformObj(memberAccountDto,MemberAccountEntity.class);
        if(memberAccountService.updateById(memberAccountEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员账户表删除
     * @param id
     * @return
     */
    @ApiOperation("会员账户表删除")
    @PostMapping("/member/account/memberAccount/delete")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:delete")
    public RespBody<String> deleteMemberAccount(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberAccountService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 会员账户表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("会员账户表批量删除")
    @PostMapping("/member/account/memberAccount/deleteBatch")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:deleteBatch")
    public RespBody<String> deleteMemberAccountBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberAccountService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员账户表详情
     * @param id 会员账户表id
     * @return
     */
    @ApiOperation("会员账户表详情")
    @GetMapping("/member/account/memberAccount/info")
    @ResponseBody
    @RequiresPermissions("member:account:memberAccount:info")
    public RespBody<MemberAccountDto> infoMemberAccount(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberAccountDto memberAccountDto =  memberAccountService.getByIdExtends(id);

        return RespBody.data(memberAccountDto);
    }


}
